package com.fgr.service.Impl;

import com.fgr.beans.Adapter;
import com.fgr.beans.Animal;
import com.fgr.mapper.AdapterMapper;
import com.fgr.mapper.AnimalMapper;
import com.fgr.service.AdapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdapterServiceImpl implements AdapterService {
    @Autowired
    AdapterMapper adapterMapper;

    @Autowired
    AnimalMapper animalMapper;

    @Override
    public List<Adapter> selectAllAdapterAndUser(Integer publish) {
        List<Adapter> adapters = adapterMapper.selectAllAdapterAndUser(publish);
        return adapters;
    }

    @Override
    public String insertAdapterAuditing(Integer anId, HttpSession httpSession) {
        //判断用户是否登录
        Object obj = httpSession.getAttribute("userid");
        if (obj==null){
            return "Login";
        }
        //判断用户是否有审核未通过的领养申请
        String userid = obj.toString();
        Adapter ad = adapterMapper.selectAdapterByUserId(Integer.parseInt(userid));
        if (ad!=null){
            return "Exist";
        }

        Adapter adapter = new Adapter();
        adapter.setAdAnid(anId);

        adapter.setAdUid(Integer.parseInt(userid));
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        adapter.setAdTime(formatter.format(date));

        Integer integer = adapterMapper.insertAdapterAuditing(adapter);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }

    @Override
    public String addAdapterBg(Adapter adapter) {
        adapter.setAdPublish(1);
        Integer integer = adapterMapper.addAdapterBg(adapter);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }

    @Override
    public Adapter selectAdapterByAdId(Integer adId) {
        Adapter adapter = adapterMapper.selectAdapterByAdId(adId);
        return adapter;
    }

    @Override
    public Adapter selectAdapterAndUserByAdIdAuditing(Integer adId) {
        Adapter adapter = adapterMapper.selectAdapterAndUserByAdIdAuditing(adId);
        return adapter;
    }

    @Override
    public String updateAdapter(Adapter adapter) {
        Integer integer = adapterMapper.updateAdapter(adapter);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }    }

    @Override
    public String deleteAdapter(Integer ad_id) {
        Integer integer = adapterMapper.deleteAdapter(ad_id);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }

    @Override
    public String updateAdapterPublishByAdId(Integer adId) {
        Integer integer = adapterMapper.updateAdapterPublishByAdId(adId);
        if (integer>0){
            return "SUCCESS";
        }else {
            return "ERRO";
        }
    }
}
